package com.mike.uaa.web;

import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.util.ObjectUtils;

import java.io.IOException;
import java.util.*;

/**
 * @author z zhang
 */
public abstract class ApplicationContextUtils {
    /**
     * -- GETTER --
     * get spring application context
     */
    private static ApplicationContext appCtx;

    /**
     * Get bean of beanName
     *
     * @param beanName beanName
     * @param clazz    The corresponding class of the bean
     * @param <T>      Bean type obtained
     * @return Return bean
     */
    public static <T> T getBean(String beanName, Class<T> clazz) {
        return appCtx.getBean(beanName, clazz);
    }

    /**
     * Obtain the corresponding bean through class
     *
     * @param clazz The corresponding class of the bean
     * @param <T>   Bean type obtained
     * @return Return bean
     */
    public static <T> T getBean(Class<T> clazz) {
        return appCtx.getBean(clazz);
    }


    public static Object getBean(String name, Object... args) {
        return appCtx.getBean(name, args);

    }

    /**
     * Obtain the corresponding bean list through class
     *
     * @param clazz The corresponding class of the bean
     * @param <T>   Bean type obtained
     * @return Return bean list
     */
    public static <T> List<T> getBeansOfType(Class<T> clazz) {
        Map<String, T> beansOfType = appCtx.getBeansOfType(clazz);
        if (ObjectUtils.isEmpty(beansOfType)) {
            return Collections.emptyList();
        }
        Collection<T> collection = beansOfType.values();
        if (collection.isEmpty()) {
            return Collections.emptyList();
        }
        return new ArrayList<>(collection);
    }

    /**
     * Obtain the corresponding bean map through class
     *
     * @param clazz The corresponding class of the bean
     * @param <T>   Bean type obtained
     * @return Return bean map
     */
    public static <T> Map<String, T> getBeansMapOfType(Class<T> clazz) {
        return appCtx.getBeansOfType(clazz);
    }

    public static Environment getEnvironment() {
        return appCtx.getEnvironment();
    }

    public static boolean isPrototype(String beanName) {
        return appCtx.isPrototype(beanName);
    }

    public static boolean isSingleton(String beanName) {
        return appCtx.isSingleton(beanName);
    }

    public static Resource getResource(String location) {
        return appCtx.getResource(location);
    }

    public static Resource[] getResources(String locationPattern) throws IOException {
        return appCtx.getResources(locationPattern);
    }

    public static boolean containsBean(String beanName) {
        return appCtx.containsBean(beanName);
    }

    public static ApplicationContext getContext() {
        return appCtx;
    }

    public static void setApplicationContext(ApplicationContext applicationContext) {
        ApplicationContextUtils.appCtx = applicationContext;
    }
}
